package com.yc.bran.snack.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yc.bran.snack.Repository.ReplyCommentRepository;
import com.yc.bran.snack.bean.Comment;
import com.yc.bran.snack.bean.ReplyComment;
import com.yc.bran.snack.dto.CommentDTO;
import com.yc.bran.snack.dto.ReplyCommentDTO;
import com.yc.bran.snack.enums.ResultEnmu;
import com.yc.bran.snack.service.impl.CommentServiceImpl;
import com.yc.bran.snack.vo.ResultVo;

@RestController
@RequestMapping("/comment")
public class CommentController {

	@Autowired  
	private CommentServiceImpl commentService;

	@Autowired  
	private ReplyCommentRepository replyCommentRepository;

	@GetMapping("/getcomment")  
	public ResultVo getAllComments(Integer gid) {  
		List<Comment> comments = commentService.getCommentsByGid(gid);  
		List<ReplyComment> replyComments = commentService.getReplycommentsByGid(gid);  
		if (comments == null || comments.isEmpty()) {  
			return new ResultVo(ResultEnmu.DATA_NULL);  
		}
		ResultVo resultVo = new ResultVo(ResultEnmu.SUCCESS);  
		resultVo.setData(Map.of("comments", comments, "replyComments", replyComments));  
		return resultVo;  
	}
	// 添加评论
	@PostMapping("/addcomment")
	public ResultVo<Comment> addComment(@RequestBody CommentDTO commentDTO) {  
		Comment savedComment = commentService.addComment(commentDTO);  
		return new ResultVo(ResultEnmu.SUCCESS, savedComment);
	}

	// 添加回复评论
	@PostMapping("/addreplycomment")
	public ResultVo<ReplyComment> addReplyComment(@RequestBody ReplyCommentDTO replyCommentDTO) {  
		ReplyComment replySavedComment = commentService.addReplyComment(replyCommentDTO);
		return new ResultVo(ResultEnmu.SUCCESS, replySavedComment);  
	}
}
